package com.head.firtst.ch2.demo2;

import com.head.firtst.ch2.demo1.DisplayElement;

import java.util.Observable;
import java.util.Observer;

/**
 * @author guohuixin
 * @date 2018/8/11
 */
public class CurrentCoditionDisplay implements Observer, DisplayElement {
    private float temp;
    private float humidity;
    private Observable observable;

    public CurrentCoditionDisplay(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    public void display() {
        System.out.println("temp:" + temp + " humidity:" + humidity);
    }

    public void update(Observable o, Object arg) {
        if (o instanceof  WeatherData) {
            WeatherData weatherData = (WeatherData) o;
            this.temp = weatherData.getTemp();
            this.humidity = weatherData.getHumidity();
            display();
        }
    }
}
